/**  
* @Title: Ehcache.java
* @Package cn.net.mtons.web
* @Description: TODO
* @author ma jinjian  
* @date 2016年2月1日 下午9:45:26
* @version V1.0  
*/ 
package cn.net.mtons.web;

import java.net.URL;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * @ClassName: Ehcache
 * @Description: ehcache核心类
 * @author ma jinjian
 * @date 2016年2月1日 下午9:45:26
 *
 */
public class Ehcache {  
	  
    private static final String path = "/ehcache.xml";  
  
    private URL url;  
  
    private CacheManager manager;  
  
    private static Ehcache ehCache;  
  
    private Ehcache(String path) {  
        url = getClass().getResource(path);  
        manager = CacheManager.create(url);  
    }  
  
    public static Ehcache getInstance() {  
        if (ehCache== null) {  
            ehCache= new Ehcache(path);  
        }  
        return ehCache;  
    }  
  
    public void put(String cacheName, String key, Object value) {  
        Cache cache = manager.getCache(cacheName);  
        Element element = new Element(key, value);  
        cache.put(element);  
    }  
  
    public Object get(String cacheName, String key) {  
        Cache cache = manager.getCache(cacheName);  
        Element element = cache.get(key);  
        return element == null ? null : element.getObjectValue();  
    }  
  
    public Cache get(String cacheName) {  
        return manager.getCache(cacheName);  
    }  
  
    public void remove(String cacheName, String key) {  
        Cache cache = manager.getCache(cacheName);  
        cache.remove(key);  
    }  
  
}  
